#! /bin/sh /usr/share/dpatch/dpatch-run
## 04_lazbuild.dpatch by Mazen Neifer <mazen.neifer@supaero.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: This patch fixes lazbuild tool making it generate LRS files
## DP: from LFM files.

@DPATCH@
Index: ide/lazbuild.lpi
===================================================================
--- lazarus~/ide/lazbuild.lpi
+++ lazarus/ide/lazbuild.lpi
@@ -2,7 +2,7 @@
 <CONFIG>
   <ProjectOptions>
     <PathDelim Value="/"/>
-    <Version Value="5"/>
+    <Version Value="6"/>
     <General>
       <SessionStorage Value="InIDEConfig"/>
       <MainUnit Value="0"/>
@@ -22,10 +22,10 @@
     </RunParams>
     <RequiredPackages Count="2">
       <Item1>
-        <PackageName Value="SynEdit"/>
+        <PackageName Value="IDEIntf"/>
       </Item1>
       <Item2>
-        <PackageName Value="IDEIntf"/>
+        <PackageName Value="SynEdit"/>
       </Item2>
     </RequiredPackages>
     <Units Count="3">
Index: ide/lazbuild.lpr
===================================================================
--- lazarus~/ide/lazbuild.lpr
+++ lazarus/ide/lazbuild.lpr
@@ -437,6 +437,9 @@
   
   if Project1.MainUnitInfo=nil then
     Error(ErrorBuildFailed,'project has no main unit');
+    
+  Project1.CompilerOptions.TargetOS:=OSOverride;
+  Project1.CompilerOptions.LCLWidgetType:=WidgetSetOverride;
 
   if not SkipDependencies then begin
     // compile required packages
@@ -456,6 +459,9 @@
     end;
   end;
   
+  // update all lrs files
+  MainBuildBoss.UpdateProjectAutomaticFiles;
+
   WorkingDir:=Project1.ProjectDirectory;
   SrcFilename:=CreateRelativePath(Project1.MainUnitInfo.Filename,WorkingDir);
 
Index: ide/basebuildmanager.pas
===================================================================
--- lazarus~/ide/basebuildmanager.pas
+++ lazarus/ide/basebuildmanager.pas
@@ -68,6 +68,8 @@
                                     ): TModalResult; virtual; abstract;
 
     function BackupFile(const Filename: string): TModalResult; virtual; abstract;
+
+    function UpdateProjectAutomaticFiles: TModalResult; virtual; abstract;
   end;
 
 var
Index: ide/buildmanager.pas
===================================================================
--- lazarus~/ide/buildmanager.pas
+++ lazarus/ide/buildmanager.pas
@@ -136,6 +136,9 @@
                                     ): TModalResult; override;
     function BackupFile(const Filename: string): TModalResult; override;
 
+    function UpdateLRSFromLFM(const LRSFilename: string): TModalResult;
+    function UpdateProjectAutomaticFiles: TModalResult; override;
+
     // methods for building
     procedure SetBuildTarget(const TargetOS, TargetCPU, LCLWidgetType: string);
     procedure SetBuildTargetIDE;
@@ -956,6 +959,43 @@
   until Result<>mrRetry;
 end;
 
+function TBuildManager.UpdateLRSFromLFM(const LRSFilename: string
+  ): TModalResult;
+var
+  LFMFilename: String;
+begin
+  Result:=mrOk;
+  // check if there is a .lrs file
+  if LRSFilename='' then exit;
+  if not FilenameIsAbsolute(LRSFilename) then exit;
+  LFMFilename:=ChangeFileExt(LRSFilename,'.lfm');
+  if LRSFilename=LFMFilename then exit;
+  // check if there is a .lfm file
+  if not FileExists(LFMFilename) then exit;
+  // check if .lrs file is newer than .lfm file
+  if FileExists(LRSFilename) and (FileAge(LFMFilename)<=FileAge(LRSFilename))
+  then exit;
+  debugln('TBuildManager.UpdateLRSFromLFM ',LRSFilename,' LFMAge=',dbgs(FileAge(LFMFilename)),' LRSAge=',dbgs(FileAge(LRSFilename)));
+  // the .lrs file does not exist, or is older than the .lfm file
+  // -> update .lrs file
+  Result:=ConvertLFMToLRSFileInteractive(LFMFilename,LRSFilename);
+end;
+
+function TBuildManager.UpdateProjectAutomaticFiles: TModalResult;
+var
+  AnUnitInfo: TUnitInfo;
+begin
+  AnUnitInfo:=Project1.FirstPartOfProject;
+  while AnUnitInfo<>nil do begin
+    if AnUnitInfo.HasResources then begin
+      Result:=UpdateLRSFromLFM(AnUnitInfo.ResourceFileName);
+      if Result=mrIgnore then Result:=mrOk;
+      if Result<>mrOk then exit;
+    end;
+    AnUnitInfo:=AnUnitInfo.NextPartOfProject;
+  end;
+end;
+
 function TBuildManager.MacroFuncMakeExe(const Filename: string;
   const Data: PtrInt; var Abort: boolean): string;
 var
